<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_kode_transaksi extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_kode_transaksi_model', 'KT');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Kode Transaksi";
		viewByTemplate('kst_kode_transaksi/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->KT->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("kt_kode") == '') {
			$error->Add("kt_kode", "Kode Transaksi", $tpl_empty);
		}else if (post("kt_nama") == '') {
			$error->Add("kt_nama", "Nama Transaksi", $tpl_empty);
		}else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM kst_kode_transaksi WHERE kt_kode = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kt_kode"))))."'")) > 0) {
	 		$error->Add("kt_kode", "Kode Transaksi", $tpl_duplicate);
	 	}
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$query = $this->db->query("SELECT kt_id FROM (SELECT * FROM kst_kode_transaksi ORDER BY kt_id DESC) WHERE rownum <= 1");
				if ($query->row_array()) {
					$getquery = $query->row_array();
					$rowcount = $getquery['kt_id'];
					$autonum = $rowcount + 1;
				} else {
					$autonum = 1;
				}

	 			$data = array(
					'kt_id' => $autonum,
					'kt_kode' => $this->input->post('kt_kode'),
					'kt_nama' => $this->input->post('kt_nama'),
					'kt_kategori' => $this->input->post('kt_kategori'),
					);
				
				if (post("mode") == "create")
				{
					$this->KT->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->KT->Update($data, array('kt_kode'	=> $data['kt_kode']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kt_kode	= get('kt_kode');
		
		if ($kt_kode != "")
		{
			// Delete Single
			$arr_rule	= array('kt_kode' => $kt_kode);
			$ls_result = $this->KT->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kt_kode' => $row['kt_kode']);
				$ls_result .= $this->KT->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>